package com.resou.reader.mine.presenter;

import android.arch.lifecycle.LifecycleOwner;
import android.support.annotation.NonNull;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.SortDataBean;
import com.resou.reader.api.entry.SortSubBean;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.m.IModel;
import com.resou.reader.base.p.BasePresenter;
import com.resou.reader.utils.log.DLog;
import com.resou.reader.mine.IView.IFavoriteView;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ${Garrett} on 2018/10/2.
 * Contact me krouky@outlook.com
 */
public class FavoritePresenter extends BasePresenter<IFavoriteView, IModel> {
    private static final String TAG = "FavoritePresenter";
    private StoreService mService;
    private List<SortDataBean<List<SortSubBean>>> data;

    public FavoritePresenter(IFavoriteView view) {
        this.view = view;
        mService = ApiImp.getInstance().getService(StoreService.class);
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        super.onCreate(owner);
        mService.appCategory().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .as(this.<Result<List<SortDataBean<List<SortSubBean>>>>>bindLifecycle())
                .subscribe(new Consumer<Result<List<SortDataBean<List<SortSubBean>>>>>() {
                    @Override
                    public void accept(Result<List<SortDataBean<List<SortSubBean>>>> listResult) throws Exception {
                        DLog.i(TAG, "appCategory success , code == " + listResult.getCode());
                        data = listResult.getData();
                        view.refresh(data);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DLog.i(TAG, "appCategory error , msg:" + throwable.getMessage());
                        throwable.printStackTrace();
                    }
                });
    }
}
